var columns = [
    [
        {field:'GS', title:'公司名称', width:70, align:'center'},
        {field:'BM', title:'部门名称', width:70, align:'center'},
        {field:'XSE', title:'销售额', width:100, formatter:function (value) {
            var sl = formatJe2(value, ",");
            return sl;
        }, align:'right'},
        {field:'XSCB', title:'销售成本', width:100, formatter:function (value) {
            var sl = formatJe2(value, ",");
            return sl;
        }, align:'right'},
        {field:'XSLR', title:'销售利润', width:100, formatter:function (value) {
            var sl = formatJe2(value, ",");
            return sl;
        }, align:'right'}
    ]
];

var xslrhz_jgid;


$(function () {

    //判断查询范围，如果查询范围不是全部不允许更改机构
    if ($("#xslrhz_cxfw").val() != 0) {
        xslrhz_jgid = $("#xslrhz_jg").val();
        $("#xslrhz_gsId").val($("#xslrhz_jgmc").val());
        $("#xslrhz_gsId").attr("readonly", true);
    } else {
        $("#xslrhz_gsId").jygsjgcombotree();
    }

    $("#xslrhz_kh").qyjgzcombobox();

    $("#grid_xslrhz").datagrid({
        url:$.webPath() + "/report/xslr/xslrhzList",
        toolbar:"#xslrhz_toolbar",
        rownumbers:true,
        singleSelect:true,
        collapsible:true,
        queryParams:{
            gsId:xslrhz_jgid,
            khId:$("[name='xslrhz.kh']").val(),
            xsrqStart:$("[name='xsrqStart']").val(),
            xsrqEnd:$("[name='xsrqEnd']").val(),
            xslx:$("[name='xslrhz_xslx']").val(),
            jszt:$("[name='xslrhz_jszt']").val()
        },
        fit:true,
        fitColumns:true,
        showFooter:true,
        pageNumber:1,
        columns:columns,
        onLoadSuccess:function () {
            countSumXslrHz();
        }
    });

    //查询按钮
    $("#btn_xslrhz_search").click(function () {
        xslrhz_search();
    });

    //销售利润导出
    $("#btn_xslrhz_export").click(function () {
        var gsId;
        if ($("#xslrhz_cxfw").val() != 0) {
            gsId = xslrhz_jgid;
        } else {
            gsId = $("#xslrhz_gsId").combotree("getValue");
        }
        var khId = $("[name='xslrhz.kh']").val() || "";
        var xsrqStart = $("[name='xsrqStart']").val();
        var xsrqend = $("[name='xsrqEnd']").val();
        var xslx = $("[name='xslrhz_xslx']").val();
        var jszt = $("[name='xslrhz_jszt']").val();
        window.open($.webPath() + "/report/xslr/xslrhz_export?gsId=" + gsId + "&xsrqStart=" + xsrqStart + "&xsrqEnd=" + xsrqend + "&xslx=" + xslx + "&jszt=" + jszt);
    });

    $("#xslrhz_jszt").combobox({
        onSelect:function (record) {
            if (record.value == 1) {
                $("#xslrhz_cksj_th").html("结算时间：");
            } else {
                $("#xslrhz_cksj_th").html("出库时间：");
            }
        }
    })
});


//查询票据
function xslrhz_search() {
    var gsId;
    if ($("#xslrhz_cxfw").val() != 0) {
        gsId = xslrhz_jgid;
    } else {
        gsId = $("#xslrhz_gsId").combotree("getValue");
    }

    $('#grid_xslrhz').datagrid('load',
        {
            gsId:gsId,
            khId:$("[name='xslrhz.kh']").val(),
            xsrqStart:$("[name='xsrqStart']").val(),
            xsrqEnd:$("[name='xsrqEnd']").val(),
            xslx:$("[name='xslrhz_xslx']").val(),
            jszt:$("[name='xslrhz_jszt']").val()
        }
    );
}

//资金补差单合计
function countSumXslrHz() {
    var sumXSE = 0;
    var sumXSCB = 0;
    var sumXSLR = 0;
    var data = $("#grid_xslrhz").datagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var xse = parseFloat(data.rows[i]["XSE"]);
        var xscb = parseFloat(data.rows[i]["XSCB"]);
        var xslr = parseFloat(data.rows[i]["XSLR"]);
        xse = isNaN(xse) ? 0 : xse;
        xscb = isNaN(xscb) ? 0 : xscb;
        xslr = isNaN(xslr) ? 0 : xslr;
        sumXSE += xse;
        sumXSCB += xscb;
        sumXSLR += xslr;
    }
    addFooter(sumXSE, sumXSCB, sumXSLR);
}

function addFooter(sumXSE, sumXSCB, sumXSLR) {
    var footer = null;
    footer = [
        {"GS":"合计", "XSE":formatMoneyFourWei(sumXSE), "XSCB":formatMoneyFourWei(sumXSCB), "XSLR":formatMoneyFourWei(sumXSLR)}
    ];
    if (footer != null)
        $("#grid_xslrhz").datagrid('reloadFooter', footer);
}